/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.anhndt.audiobook.utils;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;

/**
 *
 * @author Anhndt
 */
public class StringUtil {

    public static final String BLANK = "";
//    public static final String SPACE = " ";
    public static final char SPACE = ' ';
    public static final char TAB = '\t';

    public static String getMessage(String syntaxSMS) {
        //get message from systaxSMS
        //systaxSMS = message + addressNumber ---- EX : DK 6085

        int lastIndexSpace = syntaxSMS.lastIndexOf(':');
        String message = syntaxSMS.substring(0, lastIndexSpace);
        return message;
    }

    public static String getAdressNumber(String syntaxSMS) {
        //get  address Number from systaxSMS
        //systaxSMS = message + SPACE +  addressNumber ---- EX : DK 6085

        int lastIndexSpace = syntaxSMS.lastIndexOf(':');
        String addressNumber = syntaxSMS.substring(lastIndexSpace + 1);
        return addressNumber;
    }

    //Lay ra gia tri tuong ung voi 1 dau so
    //vd : 6085 -> 500d, 6185 -> 1000d...
    public static String getPrice(String addressNumber) {
        if(addressNumber == null || addressNumber.length() < 0 || addressNumber.length() > 4)
            throw new IllegalArgumentException("Invalid address number");
        char secondChar = addressNumber.charAt(1);
        if(secondChar == '0') return "500d";
        else if(secondChar == '1') return "1000d";
        else if(secondChar == '2') return "2000d";
        else if(secondChar == '3') return "3000d";
        else if(secondChar == '4') return "4000d";
        else if(secondChar == '5') return "5000d";
        else if(secondChar == '6') return "10000d";
        else if(secondChar == '7') return "15000d";
        else throw new IllegalArgumentException("Invalid address number");
    }

    public static boolean contain(String s, String[] contain) {
        if (contain == null) {
            return false;
        }
        for (int i = 0; i < contain.length; i++) {
            if (s.equals(contain[i])) {
                return true;
            }
        }
        return false;
    }

    public static String format(String s, String[] param) {
        try {
            int start, end = 0, index = 0;
            StringBuffer buff = new StringBuffer(s);
            while ((start = s.indexOf("{", end)) != -1) {
                end = s.indexOf("}", start);
                buff.delete(start, end + 1);
                buff.insert(start, param[index]);
                s = buff.toString();
                index++;
            }
            return buff.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return s;
        }
    }

    public static String[] convertVectorToString(Vector vt) {
        if (vt == null || vt.size() == 0) {
            throw new NullPointerException("Vector is not null");
        }
        String[] s = new String[vt.size()];
        vt.copyInto(s);
        return s;

//        String s[] = new String[vt.size()];
//        for (int i = 0; i < vt.size(); i++) {
//            s[i] = vt.elementAt(i).toString();
//        }
//        return s;
    }

    public static Vector convertStringToVector(Vector vt, String[] s) {
        vt = (vt != null) ? vt : new Vector();
        for (int i = 0; i < s.length; i++) {
            vt.addElement(s[i]);
        }
        return vt;
    }

//    public static boolean isEmpty(String s) {
//        if (s == null) {
//            return true;
//        }
//
//        if ((BLANK).equals(s)) {
//            return true;
//        }
//
//        return false;
//    }
//    public static boolean isBlank(String s) {
//        if (s == null) {
//            return false;
//        }
//
//        if ((BLANK).equals(s)) {
//            return true;
//        }
//
//        return false;
//    }
    public static boolean isNullOrEmpty(String s) {
        if (s == null) {
            return true;
        }
        if ((BLANK.equals(s))) {
            return true;
        }
        return false;
    }

//    public static boolean isNullOrEmpty(String[] s) {
//        for (int i = 0; i < s.length; i++) {
//            if (isNullOrEmpty(s[i])) {
//                return true;
//            }
//        }
//        return false;
//    }
    public static Vector split(String s, String separator) {
        Vector vt = new Vector();
        while (s.indexOf(separator) != -1) {
            int indexSeparator = s.indexOf(separator);
            String temp = s.substring(0, indexSeparator);
            if (temp.length() > 0) {
                vt.addElement(temp);
            }
            s = s.substring(indexSeparator + 1);
        }
        if (s.length() > 0) {
            vt.addElement(s);
        }
        return vt;
    }

    public static boolean isAnyType(String s) {
        if (s != null && s.startsWith("anyType")) {
            return true;
        }
        return false;
    }

    public static String getValue(String s) {
        return (s == null || isAnyType(s)) ? BLANK : s;
    }

    public static String replace(String sFirst, String sReplace, String sNeedReplace) {
        int index = sFirst.indexOf(sNeedReplace);
        String result = sFirst;
        if (index != -1) {
            result = sFirst.substring(0, index) + sReplace + sFirst.substring(index + sNeedReplace.length());
        }
        return result;
    }

    /**
     * read line by line of text file
     * @param is
     * @return
     */
    public static String readLine(InputStreamReader reader) throws IOException {
        // Test whether the end of file has been reached. If so, return null.
        int readChar = reader.read();
        if (readChar == -1) {
            return null;
        }

        StringBuffer string = new StringBuffer("");

        // Read until end of file or new line
        while (readChar != -1 && readChar != '\n' && readChar != ';') {
            // Append the read character to the string. Some operating systems
            // such as Microsoft Windows prepend newline character ('\n') with
            // carriage return ('\r'). This is part of the newline character
            // and therefore an exception that should not be appended to the
            // string.
            if (readChar != '\r') {
                string.append((char) readChar);
            }
            // Read the next character
            readChar = reader.read();
        }

        return string.toString();

    }
}
